var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page: 1,
        listRows: 20,
        total: 0,
        gmtStart:'',
        gmtEnd:'',
        name:'',
        order:'desc',
        title:"",
        type:'',
        idArrUnique:[],
        nochecked:[],
        state:''
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,
            title:_this.params.title,
            realName:_this.params.name,
            order:_this.params.order,
            state:_this.params.state,
            type:_this.params.type,
            gmtEnd:_this.params.gmtEnd,
            gmtStart:_this.params.gmtStart,
        };
        console.log(paramsObj);
        Api.newcooperationdeliverylist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,
                            title:_this.params.title,
                            realName:_this.params.name,
                            type:_this.params.type,
                            order:_this.params.order,
                            state:_this.params.state,
                            gmtEnd:_this.params.gmtEnd,
                            gmtStart:_this.params.gmtStart,
                        };
                        Api.newcooperationdeliverylist(Params, function(data) {
                            _this.getDataList(data.list);
                            $('input[type=checkbox][name=new]').each(function() {
                                var self=$(this);
                                _this.params.idArrUnique.forEach(function(item,index){
                                    if (self.attr('cooperationId')==item) {
                                        self.iCheck('check');
                                    }
                                });
                            });
                            if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                                 $('input[type=checkbox][name=all]').parent().addClass('checked');
                            }else{
                                $('input[type=checkbox][name=all]').parent().removeClass('checked');
                            };

                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +
                    '<td class="text-c">'+
                        '<div class="goods-checkbox">'+
                            '<div class="check-box" cooperationId="' + data[i].cooperationId + '">'+
                                '<input type="checkbox" name="new" cooperationId="' + data[i].cooperationId + '">'+
                            '</div>'+
                        '</div>'+
                    '</td>'+
                    '<td class="text-c">' + data[i].cooperationId + '</td>' +
                    '<td class="text-c">' + data[i].title + '</td>'+
                    '<td class="text-c">' + data[i].realName + '</td>';


                // if (data[i].type == 1) {
                //     html += '<td class="text-c">' + data[i].realName + '</td>'
                // } else if (data[i].type == 2) {
                //     html += '<td class="text-c">' + data[i].adminName + '</td>'
                // }

                if (data[i].state == 2) {
                    html += '<td class="text-c c-danger">未通过</td>';
                } else if (data[i].state == 1) {
                    html += '<td class="text-c">已通过</td>';
                }else if (data[i].state == 0){
                    html += '<td class="text-c">待审核</td>';
                };

                html += '<td class="text-c">' + data[i].gmtCreate + '</td>'+
                    '<td class="text-c">' +
                    '<p><a cooperationId="' + data[i].cooperationId + '"  class="handle view">查看详情</a><a cooperationId="' + data[i].cooperationId + '"  class="handle delete">删除</a></p>' +
                    '</td>' +
                    '</tr>';
            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="7" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent: function() {
        var _this = this;
        honeySwitch.init();
        $('.view').on('click', function() {
            Pub.setSession('cooperationId', $(this).attr('cooperationId'));
            window.location.href = Pub.getHtmlUrl('./cooperation-delivery-audit.html');
        });
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('input[type=checkbox][name=all]').on('ifChanged', function() {
            if ($(this).parent().hasClass('checked')) {
                $('input[type=checkbox][name=new]').iCheck('uncheck');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('unchecked');
                },100)
            } else {
                $('input[type=checkbox][name=new]').iCheck('check');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('checked');
                },100)
            }
        });
        setTimeout(function(){
             $('input[type=checkbox][name=new]').on('ifChanged', function() {
                //获取选取的并加入数组
                _this.params.idArrUnique.push($(this).attr('cooperationId'));
                //数组去重
                _this.params.idArrUnique=_this.params.idArrUnique.unique();
                // console.log(_this.params.idArrUnique);
                //获取未选取的并加入数组
                for (var i = 0; i < $('input[type=checkbox][name=new]').length; i++) {
                     if ($('input[type=checkbox][name=new]').eq(i).prop('checked')) {
                        _this.params.nochecked.remove($('input[type=checkbox][name=new]').eq(i).attr('cooperationId'));
                         _this.params.nochecked=_this.params.nochecked.unique();
                     }else{
                        _this.params.nochecked.push($('input[type=checkbox][name=new]').eq(i).attr('cooperationId'));
                        _this.params.nochecked=_this.params.nochecked.unique();
                     };
                };
                //去除x选取数组中未选取的ID
                for (var i = 0; i < _this.params.nochecked.length; i++) {
                    // console.log(nochecked[i]);
                    _this.params.idArrUnique.remove(_this.params.nochecked[i]);
                };
                if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                     $('input[type=checkbox][name=all]').parent().addClass('checked');
                }else{
                    $('input[type=checkbox][name=all]').parent().removeClass('checked');
                };
                //console.log(_this.params.idArrUnique);
            });
        },500)
        $('.delete').on('click', function(event) {
            event.preventDefault();
            var cooperationId=$(this).attr('cooperationId');
            layui.use('layer', function(){
                layer.confirm('您确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(index){
                    _this.deleteEvent(cooperationId);
                    layer.close(index);
                }, function(){

                });
            });
        });
    },
    deleteEvent:function(cooperationId){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            cooperationId:cooperationId
        };
        console.log(Params);
        Api.newcooperationdeliverylistdelete(Params, function(data) {
            _this.params.page=1;
            _this.getData();

        })
    },
    otherEvent:function(){
        var _this = this;
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#time',
                range: '~',
                done: function(value, date, endDate) {
                    console.log(date);
                    if (date.year) {
                        if (date.month < 10) {
                            date.month = "0" + date.month;
                        }
                        if (date.date < 10) {
                            date.date = "0" + date.date;
                        }
                        if (endDate.month < 10) {
                            endDate.month = "0" + endDate.month;
                        }
                        if (endDate.date < 10) {
                            endDate.date = "0" + endDate.date;
                        }
                        var gmtStart = date.year + '-' + date.month + '-' + date.date + ' 00:00:00';
                        var gmtEnd = endDate.year + '-' + endDate.month + '-' + endDate.date + ' 00:00:00';
                        _this.params.gmtStart = gmtStart;
                        _this.params.gmtEnd = gmtEnd;

                    } else {
                        _this.params.gmtStart = '';
                        _this.params.gmtEnd = '';
                    };
                }
            });
        });
        $('#normalSearch').click(function(event) {
            _this.params.name=$('#userName').val();
            _this.params.title=$('#title').val();
            _this.params.type = $('#tpSelect').val();
            _this.params.page=1;
            _this.getData();
        });
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#changeSize').on('change', function(event) {
            event.preventDefault();
            _this.params.listRows = $(this).val();
            _this.params.page=1;
            $('#page').html('');
            _this.getData();
        });
        $('#stateSelect').on('change', function(event) {
            event.preventDefault();
            _this.params.state = parseInt($(this).val());

        });
        $('#tpSelect').on('change', function(event) {
            event.preventDefault();
            _this.params.type = parseInt($(this).val());
        });
        $('#changeDesc').on('change', function(event) {
            event.preventDefault();
            if ($(this).val() !== '排序方式') {
                _this.params.order = $(this).val();
                _this.params.page=1;
                $('#page').html('');
                _this.getData();
            };
        });
        $('#allData').click(function(event) {
            var obj = {

                passport: _this.params.passport,
                page: _this.params.page,
                listRows: _this.params.listRows,
                title:_this.params.title,
                realName:_this.params.name,
                type:_this.params.type,
                order:_this.params.order,
                state:_this.params.state,
                gmtEnd:_this.params.gmtEnd,
                gmtStart:_this.params.gmtStart,
                excel: 'excel'
            };
            var dftime = $.cookie('lag-time');
            var sTimeUnix = new Date().getTime() + parseInt(dftime);
            var sTime = Pub.toTime(sTimeUnix);
            var signTxt = 'appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&' + Pub.token;
            var sign = md5(signTxt).toUpperCase();

            var param = '?appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&sign=' + sign;

            window.location.href = Pub.server + '/admin/new.cooperation.delivery.list' + param;
        });
        $('#selectData').click(function(event) {
            var cooperationIds = '';
            //console.log(_this.params.idArrUnique);
            if (_this.params.idArrUnique.length>0) {
                cooperationIds=_this.params.idArrUnique.join(",");
            }
            if (_this.params.idArrUnique.length === 0) {
                layui.use('layer', function() {
                    layer.msg('请先选择');
                });
            }else{

                // cooperationIds=cooperationIds.substring(0,cooperationIds.length-1);

                var obj = {
                    passport: _this.params.passport,
                    page: _this.params.page,
                    listRows: _this.params.listRows,
                    title:_this.params.title,
                    realName:_this.params.name,
                    type:_this.params.type,
                    order:_this.params.order,
                    state:_this.params.state,
                    gmtEnd:_this.params.gmtEnd,
                    gmtStart:_this.params.gmtStart,
                    excelId:cooperationIds,
                    excel: 'excel'
                };
                console.log(obj);
                var dftime = $.cookie('lag-time');
                var sTimeUnix = new Date().getTime() + parseInt(dftime);
                var sTime = Pub.toTime(sTimeUnix);
                var signTxt = 'appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&' + Pub.token;
                var sign = md5(signTxt).toUpperCase();

                var param = '?appId=' + Pub.appId + '&content=' + encodeURIComponent(JSON.stringify(obj)) + '&platform=' + Pub.platform + '&timestamp=' + sTime + '&version=' + Pub.version + '&sign=' + sign;

               window.location.href = Pub.server + '/admin/new.cooperation.delivery.list' + param
            }

        });
    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.otherEvent();


    }
};
var page = new Page();